﻿using System;
using System.Collections.Generic;
using System.Text;

using Torch.WL.Entity.CleanChina;
using System.Data.SqlClient;
using System.Data;

namespace Torch.WL.DataAccess.CleanChina
{
    public class TbEMAccess
    {
        public TbEMAccess()
        {

        }


        public TbEMEntity GetModel(int EMId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from Tb_EM ");
            strSql.Append(" where EMId=@EMId");
            SqlParameter[] parameters = {
					new SqlParameter("@EMId", SqlDbType.Int,4)
			};
            parameters[0].Value = EMId;

            TbEMEntity model = new TbEMEntity();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        public List<TbEMEntity> QueryEMByYear(int year)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  * from Tb_EM ");
            strSql.Append(" where publishYear=@publishYear");
            SqlParameter[] parameters = {
					new SqlParameter("@publishYear", year)
			};

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return DataTableToList(ds.Tables[0]);
        }

        private TbEMEntity DataRowToModel(DataRow row)
        {
            TbEMEntity model = new TbEMEntity();
            if (row != null)
            {
                if (row["EMId"] != null && row["EMId"].ToString() != "")
                {
                    model.EMId = int.Parse(row["EMId"].ToString());
                }
                if (row["publishYear"] != null && row["publishYear"].ToString() != "")
                {
                    model.publishYear = int.Parse(row["publishYear"].ToString());
                }
                if (row["period"] != null && row["period"].ToString() != "")
                {
                    model.period = int.Parse(row["period"].ToString());
                }
                if (row["title"] != null)
                {
                    model.title = row["title"].ToString();
                }
                if (row["Memo"] != null)
                {
                    model.Memo = row["Memo"].ToString();
                }
                if (row["imagelist"] != null)
                {
                    string[] arrStr = row["imagelist"].ToString().Split(',');
                    List<string> list = new List<string>(arrStr);
                    model.imagelist = list;
                }
                if (row["imageCount"] != null && row["imageCount"].ToString() != "")
                {
                    model.imageCount = int.Parse(row["imageCount"].ToString());
                }
                if (row["createTime"] != null && row["createTime"].ToString() != "")
                {
                    model.createTime = DateTime.Parse(row["createTime"].ToString());
                }
            }
            return model;
        }

        
        /// <summary>
        /// 获得数据列表
        /// </summary>
        private List<TbEMEntity> DataTableToList(DataTable dt)
        {
            List<TbEMEntity> modelList = new List<TbEMEntity>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                TbEMEntity model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }


    }
}
